//
//  OutInDelayTimeViewController.m
//  LFCWarmSystem
//
//  Created by Kill on 16/4/29.
//  Copyright © 2016年 李福才. All rights reserved.
//

#import "OutInDelayTimeViewController.h"

@interface OutInDelayTimeViewController ()<UIPickerViewDataSource,UIPickerViewDelegate,MFMessageComposeViewControllerDelegate>{
    
}

@end

@implementation OutInDelayTimeViewController
@synthesize lblTime;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self uiInit];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)uiInit{
    _lblHeadView = [[UILabel alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height/5.0)];
    _lblHeadView.text = NSLocalizedString(@"OutInDelayTime_Sample", @"出入延迟时间");
    _lblHeadView.textColor = COLOR_MAINHEAD;
    _lblHeadView.font = TABLEHEADFONT;
    _lblHeadView.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_lblHeadView];
    
    lblTime = [UIButton buttonWithType:UIButtonTypeCustom];
    lblTime.frame = CGRectMake(10*WIDTHSCALE, _lblHeadView.frame.size.height+_lblHeadView.frame.origin.y, _lblHeadView.frame.size.width-20*WIDTHSCALE, TABLEVIEWCELLSMALLHEIGHT) ;
    [lblTime setTitle: [NSString stringWithFormat:@"0%@",NSLocalizedString(@"Second_Sample", @"秒")] forState:UIControlStateNormal];
    lblTime.titleLabel.font = TABLEHEADFONT;
    lblTime.layer.cornerRadius = 5.0;
    lblTime.layer.masksToBounds = YES;
    [lblTime setBackgroundColor:TEXTFIELDCOLOR];
    [self.view addSubview:lblTime];
    
    
    UIPickerView *pickeV = [[UIPickerView alloc] initWithFrame:CGRectMake(0, MAINSCREENBOUNDS.size.height*2/3.0, MAINSCREENBOUNDS.size.width, MAINSCREENBOUNDS.size.height/3.0)];
    pickeV.delegate = self;
    pickeV.dataSource = self;
    [self.view addSubview:pickeV];
}


#pragma mark-UIPickerViewDateSource
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return 300;
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [NSString stringWithFormat:@"%ld %@",(long)row,NSLocalizedString(@"Second_Sample", @"秒")];
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 44;
}
#pragma mark-UIPickerViewDelegate
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view{
    UILabel *lblPickTime = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    lblPickTime.text = [NSString stringWithFormat:@"%ld%@",(long)row,NSLocalizedString(@"Second_Sample", @"秒")];
    lblPickTime.textAlignment = NSTextAlignmentCenter;
    lblPickTime.font = TABLESECTIONFONT;
    if (row == 5) {
        [pickerView selectRow:row inComponent:0 animated:YES];
        lblPickTime.font = PICKVIEWCELLFONT;
        [lblTime setTitle:[NSString stringWithFormat:@"%ld%@",(long)row,NSLocalizedString(@"Second_Sample", @"秒")] forState:UIControlStateNormal] ;
    }
    return lblPickTime;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    UILabel *lbl = (UILabel *)[pickerView viewForRow:row forComponent:component];
    lbl.font = PICKVIEWCELLFONT;
    [lblTime setTitle:[NSString stringWithFormat:@"%ld%@",(long)row,NSLocalizedString(@"Second_Sample", @"秒")] forState:UIControlStateNormal] ;
}

-(void)rightAction:(id)sender{
    NSString *msgContect = [self languageMSG:@"Entry and exit delay time(0-300 sec):"];
    msgContect = [NSString stringWithFormat:@"%@:\n%@",msgContect,lblTime.titleLabel.text];
    [self showSMSPicker:msgContect];
}

- (NSString *)languageMSG:(NSString*)content{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"mulLan" ofType:@"plist"];
    NSDictionary *data = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
        NSString *str = @"";
    if ((str = [data objectForKey:[NSString stringWithFormat:@"CDelayTimeSMS_%@ ",self.modelPhone.language]])) {
        content = str;
        
    }
    return [[content componentsSeparatedByString:@"\\r"] firstObject];
}
#pragma mark-Send Message
-(void)showSMSPicker:(NSString *)MSGContent{
    Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
    if (messageClass != nil) {
        // Check whether the current device is configured for sending SMS messages
        if ([messageClass canSendText]) {
            MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
            picker.recipients = [NSArray arrayWithObjects:self.modelPhone.phoneNumber, nil];
            picker.messageComposeDelegate = self;
            picker.body = MSGContent;
            [self presentViewController:picker animated:YES completion:^{
            }];
        }
        else {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"DeviceCantMSG", @"设备没有短信功能")  message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"Close_Sample", @"  关闭") otherButtonTitles: nil];
            [alert show];
        }
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"VersionProblem", @"iOS版本过低,iOS4.0以上才支持程序内发送短信") message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"Close_Sample", @"  关闭") otherButtonTitles: nil];
        [alert show];
    }
}
//
//#pragma mark-MFMessageComposeViewControllerDelegate
//- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
//    
//    switch (result)
//    {
//        case MessageComposeResultCancelled:
//            break;
//        case MessageComposeResultSent:
//            break;
//        case MessageComposeResultFailed:{
//            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"SendMSGFail", @"短信发送失败")  message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"Close_Sample", @"  关闭") otherButtonTitles: nil];
//            [alert show];
//        }
//            break;
//        default:
//            break;
//    }
//    [self dismissViewControllerAnimated:YES completion:^{}];
//}

@end
